package com.leji.song.mainPage;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import com.dgl.sdk.util.JsonUtil;
import com.dgl.sdk.view.LoadImage;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.leji.song.R;
import com.leji.song.extra.BaseActivity;
import com.leji.song.extra.BaseCoreActivity;
import com.leji.song.extra.LoginActivity;
import com.leji.song.extra.MyApplication;
import com.leji.song.httpInterface.BaseAjaxParams;
import com.leji.song.httpInterface.HttpInterface;
import com.leji.song.httpInterface.NomalResult;
import com.leji.song.mainPage.model.*;
import com.leji.song.util.MyAjaxCallBack;
import net.tsz.afinal.FinalHttp;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by mac on 2014/6/14.
 */
public class ShopMenuActivity extends BaseActivity{

    protected ListView foodListView;
    protected ListView goodsTypeListView;
    protected TextView translatePriceInfo;
    protected TextView money;
    protected Button submit;

    private String shopID;

    protected List<DS_Goods> buyGoodsResultList = new ArrayList<DS_Goods>();

    List<DS_TranslatePrice> tPriceList = new ArrayList<DS_TranslatePrice>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setAnimType(RIGHT_ENTER_LEFT_EXT);
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.shop_menu);
        initView();

        shopID = getIntent().getStringExtra("shopID");

        getMenu(null);
        getGoodsType();
        getTranslatePriceInfo();
    }



    protected void initView() {
        setActivityTitle(getIntent().getStringExtra("shopName"));
        setReturnBtn();
        setRightBtn(R.drawable.shop_icon, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ShopMenuActivity.this, ShopDetailActivity.class);
                intent.putExtra("shopID", shopID);
                intent.putExtra("shopName", getIntent().getStringExtra("shopName"));
                startActivity(intent);
            }
        });
        foodListView = (ListView) findViewById(R.id.foodListView);
        goodsTypeListView = (ListView) findViewById(R.id.goodsTypeListView);

        translatePriceInfo = (TextView) findViewById(R.id.translatePriceInfo);
        money = (TextView) findViewById(R.id.money);
        submit = (Button) findViewById(R.id.submit);

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(buyGoodsResultList.size()==0){
                    showToast("请选择菜品！");
                }else{

                    if(MyApplication.isLogin){
                        Intent intent = new Intent(ShopMenuActivity.this, SubmitOrderActivity.class);
                        intent.putExtra("shopID", shopID);
                        intent.putExtra("fromWho", "food");
                        intent.putExtra("orderList", (Serializable) buyGoodsResultList);
                        intent.putExtra("translatePrice", (Serializable) tPriceList);
                        startActivity(intent);
                    }else{
                        showActivity(LoginActivity.class);
                    }

                }
            }
        });
    }

    protected void getMenu(String type) {

        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetShopMenu.params.shopsId, shopID);
        params.put(HttpInterface.HIF_GetShopMenu.params.size, "20");
        params.put(HttpInterface.HIF_GetShopMenu.params.no, "1");
        if(type!=null){
            params.put(HttpInterface.HIF_GetShopMenu.params.type, type);
        }

        FinalHttp fh = new FinalHttp();
        fh.get(HttpInterface.HIF_GetShopMenu.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)this) {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);

                Object obj = NomalResult.doResult(o,ShopMenuActivity.this);
                if (obj != null) {

                    Gson gson = new Gson();

                    DS_NormalResult result = JsonUtil.fromJson(gson.toJson(obj), DS_NormalResult.class);

                    try {
                        Object dataObj = result.getDatas();
                        if(dataObj!= null){
                            Type listType = new TypeToken<List<DS_Goods>>() {}.getType();
                            List<DS_Goods> goodsList = gson.fromJson(gson.toJson(dataObj), listType);

                            GoodsAdapter goodsAdapter = new GoodsAdapter(ShopMenuActivity.this, goodsList, new GoodsAdapter.BuyGoodsCallback() {
                                @Override
                                public void onClick(DS_Goods goods) {
                                    calcBuyGoodsResult(goods);
                                }
                            });
                            foodListView.setAdapter(goodsAdapter);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }
            }
        });
    }

    protected void calcBuyGoodsResult(DS_Goods goods) {

        boolean isHave =false;
        for(DS_Goods goods1: buyGoodsResultList){

            if(goods1.getId() == goods.getId()){
                goods1.setBuyNum(goods.getBuyNum());
                isHave = true;
                break;
            }
        }

        if(!isHave){
            buyGoodsResultList.add(goods);
        }

        showTotalPrice(buyGoodsResultList);
    }

    private void showTotalPrice(List<DS_Goods> buyGoodsResultList) {
        float totalMoney = 0.0f;
        for(DS_Goods goods: buyGoodsResultList){
            totalMoney += goods.getBuyNum()*Float.valueOf(goods.getPrice());
        }

        money.setText(Float.toString(totalMoney));


        for(DS_TranslatePrice price:tPriceList){
            if(totalMoney < Float.valueOf(price.getS_price())){
                translatePriceInfo.setText(price.getName());
                break;
            }
        }

//        if(totalMoney<50.0f){
//            translatePriceInfo.setText("运费10元，还差"+(Float.toString(50.0f-totalMoney))+ "元5元运费");
//        }else if(totalMoney < 100.0f){
//            translatePriceInfo.setText("运费5元，还差"+(Float.toString(100.0f-totalMoney))+ "元免运费");
//        }else {
//            translatePriceInfo.setText("免运费");
//        }
    }


    protected void getGoodsType() {

        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetShopGoodsType.params.shopsId, shopID);

        FinalHttp fh = new FinalHttp();
        fh.get(HttpInterface.HIF_GetShopGoodsType.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)this) {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
                Object obj = NomalResult.doResult(o, ShopMenuActivity.this);
                if (obj != null) {

                    Gson gson = new Gson();


                    try {
                        Type listType = new TypeToken<List<DS_GoodsType>>() {
                        }.getType();

                        List<DS_GoodsType> allTypeList = new ArrayList<DS_GoodsType>();
                        DS_GoodsType allType = new DS_GoodsType();
                        allType.setName("全部");
                        allType.setId(null);  //id 为空， 拿所有的。

                        allTypeList.add(allType);
                        List<DS_GoodsType> typesList = gson.fromJson(gson.toJson(obj), listType);

                        allTypeList.addAll(typesList);
                        GoodsTypeAdapter adapter = new GoodsTypeAdapter(ShopMenuActivity.this,
                                allTypeList, new GoodsTypeAdapter.GoodsTypeClickCallback() {
                            @Override
                            public void onClick(String typeID) {
                                getMenuByID(typeID);
                            }
                        });

                        goodsTypeListView.setAdapter(adapter);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    protected void getMenuByID(String typeID) {

        getMenu(typeID);
    }


    protected void getTranslatePriceInfo() {
        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetShopDetail.params.shopsId, shopID);

        FinalHttp fh = new FinalHttp();
        fh.get(HttpInterface.HIF_GetShopDetail.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)this) {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);

                Object obj = NomalResult.doResult(o,ShopMenuActivity.this);
                if (obj != null) {

                    try {

                        Gson gson = new Gson();
                        DS_ShopInfoDetail shopDetail = JsonUtil.fromJson(gson.toJson(obj), DS_ShopInfoDetail.class);


                        Object obj2 = shopDetail.getPricePackage();

                        if (obj2 != null) {

                            Gson gson2 = new Gson();
                            Type listType = new TypeToken<List<DS_TranslatePrice>>() {
                            }.getType();

                            tPriceList = gson2.fromJson(gson.toJson(obj2), listType);

                            if(tPriceList.size()>0){
                                translatePriceInfo.setText(tPriceList.get(0).getName());
                            }
                        }

                    }catch (Exception e){
                        e.printStackTrace();
                    }



                }
            }
        });

    }
}

